﻿using NHibernate;
using System.IO;
using System.Web.Mvc;
using Web.Model;
using System.Drawing;

namespace Web.HelperClasses.AvatarService
{
    public class NewsAvatarService:AvatarService
    {
        [HttpPost]
        public override void UploadNew(int Id, Stream avatarStream)
        {
            var news = session.Get<News>(Id);
            var avatarImg = new Bitmap(avatarStream);

            var smallSized = ResizeImage(avatarImg, width: 64);
            var largeSized = ResizeImage(avatarImg, width: 192);

            string smallEditUrl = yandexFotki.GetOrCreateAlbum("Small avatars");
            string largeEditUrl = yandexFotki.GetOrCreateAlbum("Large avatars");
            var small = yandexFotki.UploadPhotoWithName(smallSized, smallEditUrl, news.AlbumName);
            var large = yandexFotki.UploadPhotoWithName(largeSized, largeEditUrl, news.AlbumName);

            if (news.NewsAvatar != null)
                RemoveAvatar(news.NewsAvatar);
            news.NewsAvatar = new Avatar
            {
                Small = small,
                Large = large
            };
        }

        public NewsAvatarService(ISession session, YandexFotki yandexFotki)
            :base(yandexFotki)
        {
            this.session = session;
        }

        private readonly ISession session;
    }
}